package com.me.mygdxgame;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;

import sun.util.BuddhistCalendar;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Json;

import commonsUtils.MainLogger;

public class MyGdxGame implements ApplicationListener {

	private Texture myTexture;
	private OrthographicCamera cam;
	private SpriteBatch spriteBatch;
	private TextureRegion textureregion;
	private int w;
	private int h;
	private InputMultiplexer multiPlexer;
	private Pumpkit pump;
	private Preferences preferences;
	private FileHandle fileHanler;
	private Player player;
	private Json json;
	private BitmapFont bitMapFont;
	private BitmapFont bmf2;
	
	@Override
	public void create() {
		this.bitMapFont = new BitmapFont();
		
		json = new Json();
		this.pump = new Pumpkit(50f ,50f);
		preferences = Gdx.app.getPreferences("PreferenciasDeUbicacion");
		w = Gdx.graphics.getWidth();
		h = Gdx.graphics.getHeight();
		this.cam = new OrthographicCamera(w,h);
		this.spriteBatch = new SpriteBatch();
		this.myTexture = new Texture(Gdx.files.internal("Untitled.png"));
		this.textureregion = new TextureRegion(this.myTexture, 0, 0, 56, 56);
		
		Gdx.input.setInputProcessor(new MyInputProcessor());
		
		this.multiPlexer = new InputMultiplexer();
		multiPlexer.addProcessor(new MyInputProcessor());

		pump.setPosX(this.preferences.getFloat("posX", 50));
		pump.setPosY(this.preferences.getFloat("posY", 50));
		
		fileHanler = Gdx.files.local("MyArchivo.bin");
		
		this.bmf2 = new BitmapFont(Gdx.files.internal("asd.fnt"),
				Gdx.files.internal("asd.png"), false);
		
		
		this.player = new Player("PEPE", 234324l, 5);
		String cadString = this.json.toJson(this.player);
		String cadStringPretty = this.json.prettyPrint(this.player);
		System.out.println("JUGADOR EN FORMATO JSON  " + cadString);
		System.out.println("JUGADOR EN FORMATO JSON PRYTTY " + cadStringPretty);
//		NO ME FUNCO PERO SE PUEDE RECUPERAR UN OBJETO DE UN JSON
//		Player dummyPLayer = this.json.fromJson(Player.class, cadString);
//		System.out.println("PLAYER sacado de un JSON  " + dummyPLayer.toString());
		this.write();
		this.read();
	}

	@Override
	public void dispose() {
		preferences.putFloat("posX", this.pump.getPosX());
		preferences.putFloat("posY", this.pump.getPosY());
		preferences.flush();
		spriteBatch.dispose();
		myTexture.dispose();
		this.bitMapFont.dispose();
		this.bmf2.dispose();
		this.write();
		
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0.3f, 0.3f, 0.7f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		spriteBatch.begin();
		
		pump.draw(spriteBatch);

		this.bitMapFont.draw(spriteBatch, "Hola Mundo", 50, 100);
		this.bmf2.drawMultiLine(spriteBatch, "Hola Mundo", 100, 120);
		this.bmf2.setColor(Color.RED);
		
		int x = Gdx.input.getX();
		int y = Gdx.input.getY();
		
		
		int dx = Gdx.input.getDeltaX();
		int dy = Gdx.input.getDeltaY();
		
		x += dx;
		y += dy;
		
		if (Gdx.input.isTouched()) {
			// spriteBatch.draw(this.textureregion, x, h -y);
			spriteBatch.draw(this.textureregion, x, h - y);
			
		}
		
		if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
			pump.setPosX(pump.getPosX() - 5f);
		}
		 if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
			pump.setPosX(pump.getPosX() + 5f);			
		}
		 if(Gdx.input.isKeyPressed(Input.Keys.UP)){
			pump.setPosY(pump.getPosY() - 5f);
		}
		 if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
			pump.setPosY(pump.getPosY() + 5f);
		}
		
		
		spriteBatch.end();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
	
	public void write() {
		ByteArrayOutputStream baos = null;
		ObjectOutputStream oos = null;
		Player p = new Player("PEPE", 32340l, 5);
		try {
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			
			// oos.writeLong(123999l);
			oos.writeObject(p);
			oos.flush();
			
			byte [] datos = baos.toByteArray();
			this.fileHanler.writeBytes(datos, false);
			
		} catch (Exception e) {
			MainLogger.LOGGER.log(Level.WARNING, e.getMessage());
			e.printStackTrace();
		}
		finally {

		}
	}
	
	public void read() {
		
		ByteArrayInputStream bais = null;
		ObjectInputStream ois = null;
		
		try {
			Player l = null;
			bais = new ByteArrayInputStream(this.fileHanler.readBytes());
			ois = new ObjectInputStream(bais);
			
			l = (Player) ois.readObject();
			
			MainLogger.LOGGER.log(Level.INFO, l.toString());
			
		} catch (Exception e) {
			MainLogger.LOGGER.log(Level.WARNING, e.getMessage());
			e.printStackTrace();
		}
	}
}
